package com.dong.mqtt;

import com.dong.mybatis.entity.Zone1;
import com.dong.mybatis.entity.Zone2;
import com.dong.mybatis.entity.Zone3;
import com.dong.mybatis.service.impl.Zone1ServiceImpl;
import com.dong.mybatis.service.impl.Zone2ServiceImpl;
import com.dong.mybatis.service.impl.Zone3ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
@Slf4j
public class EmqMsgProcess implements MqttCallback {

    @Autowired
    private Zone1ServiceImpl zone1Service;

    @Autowired
    private Zone2ServiceImpl zone2Service;

    @Autowired
    private Zone3ServiceImpl zone3Service;

public EmqMsgProcess() {
    this.threshold.put("zone1max", Arrays.asList(15.0,25.0,45.0,85.0,84.0));
    this.threshold.put("zone2max", Arrays.asList(15.0,25.0));
    this.threshold.put("zone3max", Arrays.asList(15.0,25.0));
    this.threshold.put("zone1min", Arrays.asList(5.0,5.0,5.0,6.0,7.0));
    this.threshold.put("zone2min", Arrays.asList(15.0,25.0));
    this.threshold.put("zone3min", Arrays.asList(15.0,25.0));
}

    public Map<String, List<Double>> threshold=new HashMap<>();

    private final ObjectMapper mapper = new ObjectMapper();

    @Override
    public void connectionLost(Throwable cause) {
        log.warn("⚠️ MQTT 连接断开: {}", cause.getMessage());
        // 这里不用重连逻辑，连接断开会自动重连，或者交由 EmqClient 处理
    }



    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        String payload = new String(message.getPayload());
        log.info("📩 收到消息 - Topic: {}, 内容: {}", topic, payload);
        log.info("打印出数据阈值{}",threshold.toString());
//        private Double temp;
//
//        private Double humi;
//
//        private Double a;
//
//        private Double v;
//
//        private Double l;

        try {
            switch (topic) {
                case "zone1":
                    Zone1 zone1 = mapper.readValue(payload, Zone1.class);
                    Double arr[]={zone1.getTemp(),zone1.getHumi(),zone1.getA(),zone1.getV(),zone1.getL()};
                    System.out.println("start❌");
                    for (int i = 0; i < 5; i++) {
//                        System.out.println(arr[i]);
//                        if(arr[1]<threshold.get("zone1").get(i)){
//                         log.info("第一区域出现过低 {}",threshold.get("zone1").get(i)+"  "+i+"个数据");
//                        } else if (arr[1]>threshold.get("zone1").get(i)) {
//                            log.info("第一区域出现过高");
//                        }

                        switch (i){
                            case 0:
                                    System.out.println(arr[i]);
                                if(arr[i]<threshold.get("zone1min").get(i)){
                                        log.info("第一区域温度出现过低 {},{}",threshold.get("zone1min").get(1),arr[i]);
                                    } else if (arr[i]>threshold.get("zone1").get(i)) {
                                        log.info("第一区域温度出现过高{}",threshold.get("zone1max").get(1));
                                  }
                                  break;
                            case 1:
                                    if(arr[i]<threshold.get("zone1min").get(i)){
                                        log.info("第一区域湿度出现过低 {}",arr[i]);
                                    } else if (arr[i]>threshold.get("zone1max").get(i)) {
                                        log.info("第一区域湿度出现过高{}",arr[i]);
                                    }
                                break;

                            case 2:
                                if(arr[i]<threshold.get("zone1min").get(i)){
                                    log.info("第一区域电流出现过低 {}",arr[i]);
                                } else if (arr[i]>threshold.get("zone1max").get(i)) {
                                    log.info("第一区域电流出现过高{}",arr[i]);
                                }
                                break;


                            case 3:
                                if(arr[i]<threshold.get("zone1min").get(i)){
                                    log.info("第一区域电压出现过低 {}",arr[i]);
                                } else if (arr[i]>threshold.get("zone1max").get(i)) {
                                    log.info("第一区域电压出现过高{}",arr[i]);
                                }
                                break;


                            case 4:
                                if(arr[i]<threshold.get("zone1min").get(i)){
                                    log.info("第一区域光照出现过低 {}",arr[i]);
                                } else if (arr[i]>threshold.get("zone1max").get(i)) {
                                    log.info("第一区域光照出现过高{}",arr[i]);
                                }
                                break;

                        }


                    }
                    System.out.println("end❌");
                    log.info("✅ 解析Zone1数据成功: {}", zone1.toString());
                    boolean save1 = zone1Service.save(zone1);
                    log.info(save1 ? "保存Zone1成功" : "保存Zone1失败");
                    break;
                case "zone2":
                    Zone2 zone2 = mapper.readValue(payload, Zone2.class);
                    log.info("✅ 解析Zone2数据成功: {}", zone2);
                    boolean save2 = zone2Service.save(zone2);
                    log.info(save2 ? "保存Zone2成功" : "保存Zone2失败");
                    break;
                case "zone3":
                    Zone3 zone3 = mapper.readValue(payload, Zone3.class);
                    log.info("✅ 解析Zone3数据成功: {}", zone3);
                    boolean save3 = zone3Service.save(zone3);
                    log.info(save3 ? "保存Zone3成功" : "保存Zone3失败");
                    break;
                // 可扩展其他主题处理
                default:
                    log.warn("⚠️ 未知主题: {}", topic);
            }
        } catch (Exception e) {
            log.error("❌ JSON 解析失败 - Topic: {}, 错误: {}", topic, e.getMessage());
        }
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        log.debug("✅ 消息发送完成");
    }
}
